﻿using IPC.ListConnect;
using IPC.LogCommon.DelegateAndEvent;
using IPC.LogCommon.log4net;
using IPC.WinForm.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPC.WinForm.BaseForm
{
    public partial class FrmAngular : Form
    {
        private AngularViewModel _angularViewModel;
        public FrmAngular(string name,AngularViewModel angularViewModel)
        {
            InitializeComponent();
            this.pieChart1.Series = angularViewModel.Series;
            this.pieChart1.VisualElements = angularViewModel.VisualElements;
            this.pieChart1.InitialRotation = -225;
            this.pieChart1.MaxAngle = 270;
            this.pieChart1.MinValue = 0;
            this.pieChart1.MaxValue = 100;
            this.pieChart1.Location = new Point(0, 0);
            this.pieChart1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            _angularViewModel = angularViewModel;
            DelegateAndEvent.Instance.RefreshSeriesEvent += Instance_RefreshSeriesEvent;
            this.label2.Text = name;
        }

        private delegate void RefreshAnguageDelegate(string fullName, string recordTime, string value);
        private void Instance_RefreshSeriesEvent(string fullName, string recordTime, string value)
        {
            try
            {
                if (pieChart1.InvokeRequired)
                {
                    RefreshAnguageDelegate refreshAnguageDelegate = new RefreshAnguageDelegate(Instance_RefreshSeriesEvent);
                    pieChart1.BeginInvoke(refreshAnguageDelegate, new object[] { fullName, recordTime, value });
                }
                else
                {
                    double d = value.ToDouble(0);
                    if (d > 100)
                    {
                        _angularViewModel.UpdateValue(100);
                    }
                    else
                    {
                        _angularViewModel.UpdateValue(d);
                    }
                    this.label1.Text = d.ToString();
                }
            }
            catch(Exception ex)
            {
                LogClass.Instance.Error($"仪表刷新失败:{ex.Message}");
            }
        }
    }
}
